package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.MemberPurchaseOrder;
import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.module.pojo.UserMember;
import com.example.fitnessservice.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Date;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Autowired
    private MemberService ourService;

    //查询用户的vip信息
    @GetMapping("/{userId}")
    public Result findVipInformationById(@PathVariable Integer userId) throws ParseException {
        UserMember userMember = ourService.findVipInformationById(userId);
        Result result = new Result();

        if(userMember != null){
            //该用户有会员信息
            if(userMember.getMemberStatus() == 1){
                //有会员信息，并且会员没有过期
                result.setCode(200);
                result.setMsg("会员信息查询成功");
                result.setData(userMember);
            } else {
                //有会员信息，但是会员已经过期
                result.setCode(500);
                result.setMsg("会员已过期，您还要续约吗");
            }

        }else {
            //该用户没有会员信息
            result.setCode(500);
            result.setMsg("您还没有会员，请加入我们");
        }

        return result;
    }

    //会员充值
    @PostMapping
    public Result memberRenewal(@RequestBody MemberPurchaseOrder memberPurchaseOrder) throws ParseException {
        memberPurchaseOrder.setOrderTime(new Date());
        System.out.println(memberPurchaseOrder);
        boolean flag = ourService.memberRenewal(memberPurchaseOrder);

        Result result = new Result();
        if(flag){
            result.setCode(200);
            result.setMsg("会员充值成功");
        }else {
            result.setCode(500);
            result.setMsg("会员充值失败");
        }
        return result;
    }



}
